require 'uri'
require 'csv'

module Utils
  
  def parse_field(field)
    field == "null" ? nil : field
  end
  
  def get_base_uri(uri)
    full_url = URI.parse uri
    "#{full_url.scheme}://#{full_url.host}/"
  end
  
  def mat_to_csv(mat)
    buff = String.new
    
    CSV::Writer.generate(buff) do |csv|
      mat.each {|r| csv << [r[0], sprintf("%.2f", r[1]) ]}
    end
    
    buff
  end
  
  def mat_to_latex(mat)
    buff = "\\begin{tabular}{ l c }\n"
    
    mat.each {|r| buff << "\t#{r[0]} & #{sprintf("%.2f", r[1])}] \\\\\n"}
    buff << "\\end{tabular}\n"
  end
    
    module_function :parse_field, :get_base_uri, :mat_to_latex, :mat_to_csv
end